import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
 
class SwingExample implements Runnable {
    public void run() {
        JFrame f = new JFrame ("Hello, World!");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        final JTextField usernameField = new JTextField(32);
        final JPasswordField pswField = new JPasswordField(32);    
        JPanel textControlsPane = new JPanel();
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        textControlsPane.setLayout(gridbag);
        JLabel[] labels = {new JLabel("User name: "), new JLabel("Password: ")};
        JTextField[] textFields = {usernameField, pswField};
        addLabelTextRows(labels, textFields, gridbag, textControlsPane);
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.anchor = GridBagConstraints.WEST;
        c.weightx = 1.0;
        textControlsPane.setBorder(
                BorderFactory.createCompoundBorder(
                                BorderFactory.createTitledBorder("Login"),
                                BorderFactory.createEmptyBorder(5,5,5,5)));
        f.add(textControlsPane, BorderLayout.PAGE_START);
        
        JButton button = new JButton("OK");
        button.addActionListener(new ActionListener() {
        	class MainFrameStarter implements Runnable {
        		static final private String USER_NAME = "mr";
        		static final private String PSW = "mrsoft";

				@Override
				public void run() {
					if (USER_NAME.equals(usernameField.getText()) && PSW.equals(String.valueOf(pswField.getPassword()))) {
				        JFrame f = new JFrame ("Hello, World!");
				        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				        f.getContentPane().add(new JLabel("Main window."));
				        f.pack();
				        f.setVisible(true);
					}
				}
        		
        	}

			@Override
			public void actionPerformed(ActionEvent arg0) {
		        SwingUtilities.invokeLater(new MainFrameStarter());
			}
        	
        });
        JPanel controlPanel = new JPanel();
        controlPanel.add(button);
        
        f.getContentPane().add(textControlsPane, BorderLayout.PAGE_START);
        f.getContentPane().add(controlPanel, BorderLayout.PAGE_END);
        
        f.pack();
        f.setVisible(true);
    }
 
    private void addLabelTextRows(JLabel[] labels,
                                  JTextField[] textFields,
                                  GridBagLayout gridbag,
                                  Container container) {
        GridBagConstraints c = new GridBagConstraints();
        c.anchor = GridBagConstraints.EAST;
        int numLabels = labels.length;
 
        for (int i = 0; i < numLabels; i++) {
            c.gridwidth = GridBagConstraints.RELATIVE;
            c.fill = GridBagConstraints.NONE;
            c.weightx = 0.0;
            container.add(labels[i], c);
 
            c.gridwidth = GridBagConstraints.REMAINDER;
            c.fill = GridBagConstraints.HORIZONTAL;
            c.weightx = 1.0;
            container.add(textFields[i], c);
        }
    }
 
    public static void main(String[] args) {
        SwingExample se = new SwingExample();
        SwingUtilities.invokeLater(se);
    }
}
